package config

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/mitchellh/mapstructure"
	"idle/utils/configUtls"
	"log"
)

// 职业属性修正.xlsx
type JobConfig struct {
	Sid       int `json:"Sid"`       // Sid
	Job       int `json:"Job"`       // 职业
	LvToHp    int `json:"LvToHp"`    // 每级生命提升
	LvToMana  int `json:"LvToMana"`  // 每级法力提升
	VitToHp   int `json:"VitToHp"`   // 体力转化生命
	EngToMana int `json:"EngToMana"` // 精力转化法力
	BaseHp    int `json:"baseHp"`    // 基础生命
	BaseMana  int `json:"baseMana"`  // 基础法力
	BaseStr   int `json:"baseStr"`   // 基础力量
	BaseDex   int `json:"baseDex"`   // 基础敏捷
	BaseVit   int `json:"baseVit"`   // 基础体力
	BaseEng   int `json:"baseEng"`   // 基础精力
}

var jobConfigList []*JobConfig
var jobConfigMap map[int]*JobConfig

func GetJobConfig(Sid int) *JobConfig {
	if v, ok := jobConfigMap[Sid]; ok {
		return v
	}
	return nil
}

func GetAllJobConfig() []*JobConfig {
	result := make([]*JobConfig, 0)
	for _, c := range jobConfigList {
		result = append(result, c)
	}
	return result
}

func loadJobConfig(filePath string) {
	xlsx, err := excelize.OpenFile(filePath)
	if err != nil {
		log.Fatal(err)
	}
	sheetName := xlsx.GetSheetName(1)
	rows := xlsx.GetRows(sheetName)

	jobConfigMap = make(map[int]*JobConfig)
	jobConfigList = make([]*JobConfig, 0)

	for _, row := range rows[4:] {
		data := make(map[string]interface{})
		for j, colCell := range row {
			fieldName := rows[1][j]
			if fieldName != "" {
				data[rows[1][j]] = configUtls.GetValueByType(rows[2][j], colCell)
			}
		}
		var config JobConfig
		err := mapstructure.Decode(data, &config)
		if err != nil {
			log.Fatal(err)
			return
		}

		jobConfigMap[config.Sid] = &config
		jobConfigList = append(jobConfigList, &config)
	}
}
